from enum import Enum, unique
from dataclasses import dataclass, field
from Utils import get_texture_list, logger

"""
方块种类枚举注册
"""

# 方块注册存放表
BlockRegistry = {}


@unique
class BlockType(Enum):
    Air = 0
    Grass = 1
    Dirt = 2
    Log = 3
    Leave = 4
    SnowyGrass = 5
    DiamondBlock = 6
    Wood = 7
    Glass = 8
    Wool = 9
    CobbleStone = 10
    MossyCobbleStone = 11
    TNT = 12
    CraftingTable = 13
    DarkLog = 14
    HayBlock = 15
    Podzol = 16
    Sand = 17
    SandStone = 18
    BirchLog = 19
    Stone = 20
    Poppy = 21
    Allium = 22
    AzureBluet = 23
    Dandelion = 24
    Straw = 25


@dataclass
class Block:
    block_type: BlockType = None  # 方块类型
    block_kind: Enum = None  # 方块种类
    name: str = ''  # 方块名称
    ids: int = 0  # 方块ID
    texture_id: list = field(default_factory=lambda: [0, 0, 0, 0, 0, 0])
    transparent: bool = False
    dig_time: float = 1.0
    smoothness: float = 1.0
    shape: int = 0
    is_solid: bool = True
    dig_sound: str = 'dig.stone'
    place_sound: str = 'dig.stone'

    def __post_init__(self):
        # 注册方块
        self.name = self.block_type.name
        self.ids = self.block_type.value
        BlockRegistry[self.block_type.value] = self

        # 给方块添加对应的属性
        if self.block_kind and self.block_kind in BlockKinds:
            for key, value in self.block_kind.value.items():
                setattr(self, key, value)

        self.set_texture_id()

    def set_texture_id(self):
        """设置纹理ID"""
        if len(self.texture_id) != 6:
            raise ValueError("纹理列表长度必须为6")

        self.texture_id = [get_texture_list(i) for i in self.texture_id]


# 方块种类：相同种类的方块具有相同的属性。
class BlockKinds(Enum):
    # 花朵类型方块的属性
    Flowered = {
        "transparent": True,
        "smoothness": 0.25,
        "shape": 1,
        "is_solid": False
    }


BlockList = [
    Block(BlockType.Air, texture_id=[0, 0, 0, 0, 0, 0], transparent=True, is_solid=False),
    Block(
        BlockType.Grass,
        texture_id=[19, 19, 19, 19, 18, 17],
        smoothness=0.75,
        dig_sound='dig.grass',
        place_sound='dig.grass'
    ),
    Block(BlockType.Dirt, texture_id=[17, 17, 17, 17, 17, 17], smoothness=0.75, dig_sound='dig.dirt',
          place_sound='dig.dirt'),
    Block(BlockType.Log, texture_id=[12, 12, 12, 12, 13, 13], smoothness=1.0, dig_sound='dig.wood',
          place_sound='dig.wood'),
    Block(BlockType.Leave, texture_id=[9, 9, 9, 9, 9, 9], transparent=True, dig_sound='dig.grass',
          place_sound='dig.grass'),
    Block(BlockType.SnowyGrass, texture_id=[5, 5, 5, 5, 6, 17], smoothness=0.55, dig_sound='dig.dirt',
          place_sound='dig.dirt'),
    Block(BlockType.DiamondBlock, texture_id=[1, 1, 1, 1, 1, 1]),
    Block(BlockType.Wood, texture_id=[3, 3, 3, 3, 3, 3], smoothness=0.65, dig_sound='dig.wood', place_sound='dig.wood'),
    Block(BlockType.Glass, texture_id=[20, 20, 20, 20, 20, 20], transparent=True, smoothness=2.0),
    Block(BlockType.Wool, texture_id=[21, 21, 21, 21, 21, 21], smoothness=0.65, dig_sound='dig.grass',
          place_sound='dig.grass'),
    Block(BlockType.CobbleStone, texture_id=[22, 22, 22, 22, 22, 22]),
    Block(BlockType.MossyCobbleStone, texture_id=[23, 23, 23, 23, 23, 23], smoothness=0.8),
    Block(BlockType.TNT, texture_id=[25, 25, 25, 25, 26, 24], dig_sound='dig.grass', place_sound='dig.grass'),
    Block(BlockType.CraftingTable, texture_id=[29, 28, 28, 28, 27, 3], dig_sound='dig.wood', place_sound='dig.wood'),
    Block(BlockType.DarkLog, texture_id=[30, 30, 30, 30, 31, 31], dig_sound='dig.wood', place_sound='dig.wood'),
    Block(BlockType.HayBlock, texture_id=[32, 32, 32, 32, 33, 33], dig_sound='dig.grass', place_sound='dig.grass'),
    Block(BlockType.Podzol, texture_id=[35, 35, 35, 35, 34, 17], smoothness=0.35, dig_sound='dig.dirt',
          place_sound='dig.dirt'),
    Block(BlockType.Sand, texture_id=[11, 11, 11, 11, 11, 11], smoothness=0.65, dig_sound='dig.sand',
          place_sound='dig.sand'),
    Block(BlockType.SandStone, texture_id=[15, 15, 15, 15, 14, 16]),
    Block(BlockType.BirchLog, texture_id=[7, 7, 7, 7, 8, 8], dig_sound='dig.wood', place_sound='dig.wood'),
    Block(BlockType.Stone, texture_id=[4, 4, 4, 4, 4, 4]),
    # 花朵类型方块
    Block(BlockType.Poppy, texture_id=[240, 240, 240, 240, 240, 240], block_kind=BlockKinds.Flowered,
          dig_sound='dig.grass', place_sound='dig.grass'),
    Block(BlockType.Allium, texture_id=[241, 241, 241, 241, 241, 241], block_kind=BlockKinds.Flowered,
          dig_sound='dig.grass', place_sound='dig.grass'),
    Block(BlockType.AzureBluet, texture_id=[242, 242, 242, 242, 242, 242], block_kind=BlockKinds.Flowered,
          dig_sound='dig.grass', place_sound='dig.grass'),
    Block(BlockType.Dandelion, texture_id=[243, 243, 243, 243, 243, 243], block_kind=BlockKinds.Flowered,
          dig_sound='dig.grass', place_sound='dig.grass'),
    Block(BlockType.Straw, texture_id=[244, 244, 244, 244, 244, 244], block_kind=BlockKinds.Flowered,
          dig_sound='dig.grass', place_sound='dig.grass'),
]

logger.info(f'已注册{len(BlockRegistry)}个方块')
